// 导入Village脚本
import V from "./Village";
const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    V: V = null;

    @property({displayName: "在数组中这是第几个按钮", tooltip: "在数组中这是第几个按钮", type: cc.Float})
    btn: number = 0;

    @property({displayName: "增加多少经验值", tooltip: "增加多少经验值", type: cc.Float})
    EXP: number = 0;

    @property({displayName: "增加多少金币", tooltip: "增加多少金币", type: cc.Float})
    money: number = 0;

    onLoad () {
        let self = this;

        // 获取本身button组件
        let btn = this.node.getComponent(cc.Button);

        // 绑定事件
        this.node.on(cc.Node.EventType.TOUCH_END, function () {
            // 如果按钮启用了
            if (btn.interactable == true) {
                // 增加经验值
                self.add_EXP(self.EXP);
                // 增加金币
                self.add_money(self.money);
                // 存储数据为已领取
                self.set_already_received();
            }
        }, this);

        // 获取Village脚本
        this.V = cc.find("Canvas").getComponent("Village");
    }


    // 增加金币专用函数   num为增加多少
    add_money (num: number) {
        // 获取金币数据
        let m = cc.sys.localStorage.getItem("money");
        // 如果没有金币
        if (m == null) {
            // 存储
            cc.sys.localStorage.setItem("money", num);
        } else {
            // 如果有金币那就加上num
            let money = Number(m) + num;
            cc.sys.localStorage.setItem("money", money);
        }
    }
    

    // 增加经验值专用函数   num为加多少
    add_EXP (num: number) {
        this.V.add_EXP(num);
        cc.log("增加了经验值，函数情况", this.V.add_EXP);
    }


    // 存储数据为已领取
    set_already_received () {
        // 获取数据
        let task = cc.sys.localStorage.getItem("Task");
        // 截为数组
        let s: string[] = task.split(",");

        // 设置为已领取
        s[this.btn] = "1";
        // 结合起来
        let s_ = s[0] + "," + s[1] + "," + s[2] + "," + s[3] + "," + s[4] + "," + s[5] + "," + s[6];
        // 存起来
        cc.sys.localStorage.setItem("Task", s_);
    }

}
